﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace ScarEngine
{
    public class PhysicsManager
    {

        public List<PhysicsComponent> PhysicsList;
        public List<VelocityComponent> VelocityList;

        public PhysicsManager()
        {
            PhysicsList = new List<PhysicsComponent>();
            VelocityList = new List<VelocityComponent>();
        }

        public void Update(GameTime gameTime)
        {
            #region Physics Manager

            #region Check Physics Objects
            foreach (PhysicsComponent Aphysics in PhysicsList)
            {

               // GameObject AgameObject = Aphysics.Object;
                // PositionalComponent Aposition = AgameObject.Get<PositionalComponent>();

                #region Collisions
                foreach (PhysicsComponent Bphysics in PhysicsList)
                {

                    if (!Aphysics.Equals(Bphysics))
                    {
                        if (PhysicsEngine.IsColliding(Aphysics,Bphysics))
                        {

                            //todo

                        }
                    }


                }
                #endregion

                //Apply Gravity
                PhysicsEngine.ApplyGravity(Aphysics,gameTime);

            }
            #endregion

            #endregion

            #region Velocity Manager

            foreach (VelocityComponent component in VelocityList)
            {
                if (component.Velocity != 0) // if the object isnt even moving, why run code?
                {
                    // Run movement method
                    PhysicsEngine.MoveObject(component, gameTime);
                }

            }

            #endregion

        }


        #region Component

        #region PhysicsComponents
        public void RegisterPhysicsComponent(PhysicsComponent c)
        {
            if (!PhysicsList.Contains(c))
            {
                PhysicsList.Add(c);
            }
        }

        public void UnRegisterPhysicsComponent(PhysicsComponent c)
        {
            if (PhysicsList.Contains(c))
            {
                PhysicsList.Remove(c);
            }
        }
        #endregion

        #region VelocityComponents
        public void RegisterVelocityComponent(VelocityComponent c)
        {
            if (!VelocityList.Contains(c))
            {
                VelocityList.Add(c);
            }
        }

        public void UnRegisterVelocityComponent(VelocityComponent c)
        {
            if (VelocityList.Contains(c))
            {
                VelocityList.Remove(c);
            }
        }
        #endregion

        #endregion

    }


}